package com.chuan.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by zhaowenchuan on 2018/10/
 * 主题模式 消息提供者
 * 主题模式  先订阅才能接收到消息
 *
 */
public class AppProvider {

    public static final String URL = "tcp://localhost:61616";

    public static final String TOPIC_NAME = "topic-test";

    public static void main(String[] args) throws JMSException{
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic(TOPIC_NAME);//创建目标
        MessageProducer producer = session.createProducer(destination);//创建生产者

        for (int i = 0; i < 100; i++) {
            TextMessage textMessage = session.createTextMessage("test" + (i + 1));
            producer.send(textMessage);
            System.out.println("成功发送消息" + textMessage.getText());
        }

        connection.close();
    }
}
